local content_length=tonumber(ngx.req.get_headers()['content-length'])
local method=ngx.req.get_method()
local ngxmatch=ngx.re.match


function getwafstatus()
luasql = require "luasql.mysql"
env = luasql.mysql()
conn = env:connect("fw","fw","fw","127.0.0.1",3306)
cn = conn:execute"SET NAMES UTF8"
cur = conn:execute("SELECT * FROM `zckj_system_config` where name = 'fire_close'")
row = cur:fetch({},"a")
 is_close = "1";
while row do
    is_close = row.value
    row = cur:fetch(row,"a")
end
    cn = conn:close()
    cs = env:close()
    return is_close
end


function getClientIp()
        IP = ngx.req.get_headers()["X-Real-IP"]
        if IP == nil then
                IP  = ngx.var.remote_addr
        end
        if IP == nil then
                IP  = "unknown"
        end
        return IP
end

function whiteip()
   local cIP = getClientIp()
   if cIP == "unknown" then  ngx.exit(403);   return false end
   if cIP == "127.0.0.1" then return true end
luasql = require "luasql.mysql"
env = luasql.mysql()
conn = env:connect("fw","fw","fw","127.0.0.1",3306)
cn = conn:execute"SET NAMES UTF8"
sql = string.format("SELECT * FROM `zckj_ip` where status = 1 and ip = '%s'",cIP)
cur = conn:execute(sql)
row = cur:fetch({},"a")
if row then
    cn = conn:close()
    cs = env:close()
    return true
else
    cn = conn:close()
    cs = env:close()
    ngx.exit(403);
    return false
end

end

status = getwafstatus()
if status == "1" then
whiteip()
end





